package com.zch.threaduse;

public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        //希望main线程结束后，子线程自动结束，将子线程设置为守护进程即可
        myDaemonThread.setDaemon(true);
        myDaemonThread.start();

        for (int i = 1; i <= 5 ; i ++){
            System.out.println("成为我的守护线程吧！");
            Thread.sleep(1000);
        }
    }
}

class MyDaemonThread extends Thread{
    @Override
    public void run() {
        for (;;){ //无限循环
            System.out.println("让我来守护你...");
            try {
                Thread.sleep(1000); //休眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
